package com.epoint.mobileoa.tools;


import java.io.File;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.List;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.FrameMethod;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.dialog.SuperDialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.NotificationManager;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Html;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author liyc
 * @time 2012-1-19 下午3:36:55
 * @annotation 提示框帮助类
 */
public class AlertHelp {



	public static void ShowAlert(Context con,String message,OnClickListener listener){
		new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(con.getString(R.string.prjname))
		.setMessage(message)
		.setPositiveButton("是", listener)
		.setNegativeButton("否", null)
		.create().show();
	}

	public static void ShowAlertSingleButton(Context con,String message,OnClickListener listener){
		new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(con.getString(R.string.prjname))
		.setMessage(message)
		.setPositiveButton("是", listener)
		.create().show();
	}

	public static void ShowAlertSingleButtonWithNoIcon(Context con,String message,OnClickListener listener){
		new AlertDialog.Builder(con)
		.setMessage(message)
		.setPositiveButton("是", listener)
		.create().show();
	}

	/**
	 * @作者 lilin
	 */
	public static void showAlertMenu(SuperView con, String title,
			String menuitems[], DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(title);
		builder.setItems(menuitems, listener);
		
		builder.setIcon(R.drawable.epoint2);
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

	public static void ShowAlert(Context con,String message){
		new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(con.getString(R.string.prjname))
		.setMessage(message)
		.setPositiveButton("是", null)
		.create().show();
	}

	public static void ShowAlert2(Context con,String message){
		new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(con.getString(R.string.prjname))
		.setMessage(message)
		.setPositiveButton("确定", null)
		.create().show();
	}

	public static void AlertWithTextView(Context con,int layoutid,int tvid,String title,String msg){
		title = title==null?"":title;

		if(title.equals("")){
			title = con.getString(R.string.prjname);
		}

		LayoutInflater inflater = LayoutInflater.from(con);
		LinearLayout ll = (LinearLayout) inflater.inflate(layoutid, null);
		TextView tv = (TextView) ll.findViewById(tvid);
		tv.setText(Html.fromHtml(msg));
		AlertDialog ad = new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(title)
		.setView(ll)
		.setPositiveButton("确定", null)
		.create();
		ad.show();
	}

	public static void AlertWithTextView2(Context con,String msg){
		LayoutInflater inflater = LayoutInflater.from(con);
		LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.alertwithtextview, null);
		TextView tv = (TextView) ll.findViewById(R.id.awt_tv);
		tv.setText(Html.fromHtml(msg));
		AlertDialog ad = new AlertDialog.Builder(con)
		.setIcon(R.drawable.epoint2)
		.setTitle(con.getString(R.string.prjname))
		.setView(ll)
		.setPositiveButton("确定", null)
		.create();
		ad.show();
	}

	public static void showFileAttributeAlert(Context con,File f){
		// 格式化时间字符串
		Long time = f.lastModified();
		String timestr = DateFormat.format("yyyy.MM.dd kk:mm:ss", time)
				.toString();
		String lengthinfo = f.length()/1024+"Kb";
		if(f.length()/1024==0){
			lengthinfo = f.length()+"B";
		}
//		AlertHelp.AlertWithTextView(con,
//				R.layout.alertwithtextview, R.id.awt_tv, "文件属性",
//				);
		String msg = "<font color=yellow>文件名称：</font>"+f.getName()+"<br/>"
				+"<font color=yellow>文件大小：</font>"+lengthinfo+"<br/>"
				+"<font color=yellow>创建时间：</font>"+timestr+"<br/>"
				+"<font color=yellow>文件位置：</font>"+f.getAbsolutePath();
		EpointDialog.getDialog(con, "文件属性", msg);
	}
	
	public static void showFileAttributeAlert2(Context con,File f){
		// 格式化时间字符串
		Long time = f.lastModified();
		String timestr = DateFormat.format("yyyy.MM.dd kk:mm:ss", time)
				.toString();
		String lengthinfo = f.length()/1024+"Kb";
		if(f.length()/1024==0){
			lengthinfo = f.length()+"B";
		}
		String msg = "<font color=blue>文件名称：</font>"+f.getName()+"<br/>"
				+"<font color=blue>文件大小：</font>"+lengthinfo+"<br/>"
				+"<font color=blue>创建时间：</font>"+timestr+"<br/>"
				+"<font color=blue>文件位置：</font>"+f.getAbsolutePath();
		SuperDialog sv = new SuperDialog(con);
		sv.setTitle_Epoint("文件属性")
		.setPositiveButton("确定", null)
		.setMessage(msg)
		.show();
	}

	/**
	 * @author liyc
	 * @time 2012-2-20 下午2:19:42
	 * @annotation 显示一个附件下载对话框
	 */
	public static void showAttachDownloadAlert(final SuperView con,final List<Attach> attachList,final String saveguid){

		LayoutInflater inflater = LayoutInflater.from(con);
		LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.alert_listview, null);
		ListView lv = (ListView) ll.findViewById(R.id.al_lv);
		String[] arr = new String[attachList.size()];
		for(int i=0;i<arr.length;i++){
			arr[i] = attachList.get(i).attachfilename;
		}
		ArrayAdapter<String> aa = new ArrayAdapter<String>(con,
				R.layout.lv_textview,
				arr);
		lv.setAdapter(aa);
		final AlertDialog.Builder ab = new AlertDialog.Builder(con);
		ab.setIcon(R.drawable.epoint2)
		.setTitle("附件下载")
		.setView(ll)
		.setNegativeButton("取消", null);
		final AlertDialog ad = ab.create();
		ad.show();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String url = attachList.get(position).url;
				System.out.println(url);
				String attachurl = "";
				if(!url.contains("http://")){
					attachurl = con.getString(R.string.http)+FrameMethod.getIp(con.fp, con)+"/"+con.getString(R.string.interfacename)+url;
					System.out.println(attachurl);
				}
				else{
					AlertHelp.ShowAlert(con, "大附件需要登陆OA进行下载！");
					attachurl = url;
					ad.dismiss();
					return;
				}
				ad.dismiss();
				AttachHelp.attachdownload(con,saveguid,attachList.get(position).attachfilename,attachurl);
			}
		});
	}

	public static void notHide(DialogInterface dialog){
		try { 
			Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); 
			field.setAccessible(true); 
			field.set(dialog, false);
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
	}

	public static void Hide(DialogInterface dialog){
		try {
			Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(dialog, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void showDatePickDialog(Context con,OnDateSetListener listener,String title){
		//		etFsrq.setText(year + "-" + DateHelp.switchDay((monthOfYear+1)) + "-" + DateHelp.switchDay(dayOfMonth));
		Calendar calendar = Calendar.getInstance();        
		DatePickerDialog dialog = new DatePickerDialog(
				con,
				listener,
				calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH)
				);
		title = title==null?"":title;
		if(!title.equals("")){
			dialog.setTitle(title);
		}
		dialog.setIcon(R.drawable.epoint2);
		dialog.show();
	}

	public static void showDatePickDialogWithCancel(Context con,OnDateSetListener listener,String title){
		Calendar calendar = Calendar.getInstance();        
		DatePickerDialog dialog = new DatePickerDialog(
				con,
				listener,
				calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH)
				);
		title = title==null?"":title;
		if(!title.equals("")){
			dialog.setTitle(title);
		}
		dialog.setIcon(R.drawable.epoint2);
		dialog.show();
	}

}
